package cn.gson.springboot.controller.presalecontroller;

import cn.gson.springboot.aop.MyLog;
import cn.gson.springboot.model.vo.presalevo.CategoryTree;
import cn.gson.springboot.model.pjos.*;
import cn.gson.springboot.model.service.presaleservice.PsBusinessService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**商机管理controller*/
@Controller
@RequestMapping("/buInfo")
public class PsBusinessController {
    @Autowired
    PsBusinessService businessService;//注入商机业务类

    /*查询当前用户商机列表*/
    @RequestMapping("/select")
    @ResponseBody
    public Map<String,Object> selectBusiness(Integer page, Integer size,
                                             String businessName, Integer speedId, Integer employeesId,Integer deptId){

        Page<Object> obj = PageHelper.startPage(page,size);
        businessService.selectBusiness(employeesId,deptId,businessName,speedId);
        Map<String,Object> map = new HashMap<>();
        List<BusinessInfo> businessInfoList= businessService.selectBusiness(employeesId,deptId,businessName,speedId);
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        map.put("businessInfoList",businessInfoList);
        return map;
    }

    /**特别关注商机*/
    @RequestMapping("special_business")
    @ResponseBody
    public  Map<String,Object> selectSpecialBusiness(Integer page, Integer size,
                                                     String businessName, Integer speedId,Integer employeesId,Integer deptId){

        Page<Object> obj = PageHelper.startPage(page,size);
        businessService.selectSpecialBusiness(employeesId,deptId,businessName,speedId);
        Map map = new HashMap();
        List<BusinessInfo> specialBusinessList= businessService.selectSpecialBusiness(employeesId,deptId,businessName,speedId);
        map.put("specialBusiness",obj.getResult());
        map.put("specialTotal",obj.getTotal());
        map.put("specialBusinessList",specialBusinessList);
        return map;
    }

    /*查询所有的商机进度*/
    @RequestMapping("/speed")
    @ResponseBody
    public List<SpeedInfo> speed(){
        return businessService.allSpeed();
    }

    /**自动生成商机名称*/
    @RequestMapping("/business_name")
    @ResponseBody
    public String businessName(){
        return  businessService.selectMaxBusinessName();
    }

    /**查询该员工负责的客户*/
    @RequestMapping("/customer")
    @ResponseBody
    public Map<String,Object> selectEmpCustomer(Integer page,Integer size,String cusName,Integer employeesId){
        Page<Object> obj=PageHelper.startPage(page,size);
        System.err.println("员工id："+employeesId);
        System.err.println("客户名称:"+cusName);
        businessService.selectEmpCustomer(employeesId,cusName);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",obj.getResult());
        map.put("total",obj.getTotal());
        return map;
    }
    /**查询客户对应的联系人*/
    @RequestMapping("/culinkman")
    @ResponseBody
    public List<Culinkman> culinkmen(int customerId){
        return businessService.selectCustomerLinkman(customerId);
    }

    /*类型下的品牌*/
    @RequestMapping("/category")
    @ResponseBody
    public List<CategoryTree> categoryList(){
        return  businessService.category();
    }


    /**条件查询产品*/
    @RequestMapping("/products")
    @ResponseBody
    public Map<String,Object> productsideList(Integer page,Integer size,String prName, Integer pcId, Integer pbId ){
        Page<Object> obj=PageHelper.startPage(page,size);
        businessService.productsList(prName,pcId,pbId);
        Map<String,Object> map = new HashMap<>();
        map.put("datas",obj.getResult());
        map.put("total",obj.getTotal());
        return map;
    }
    //商机新增或修改
    @MyLog(type = "商机",value = "新增了商机")
    @PostMapping("/saveBusinessInfo")
    @ResponseBody
    public int saveBusinessInfo(@RequestBody BusinessInfo businessInfo){
       return businessService.saveBusinessInfo(businessInfo);
    }
    @RequestMapping("/queryBusiness")
    @ResponseBody
    public BusinessInfo queryBusiness(int businesId){
        return businessService.queryBusiness(businesId);
    }
    /* 查询该商机下的产品详情*/
    @RequestMapping("businessDatails")
    @ResponseBody
    public List<Details> queryDetais(int businesId){
        return businessService.queryDetais(businesId);
    }

    /**
     * 商机是否为特别关注
     */
    @RequestMapping("set_special")
    @ResponseBody
    public void specialAttention(String businessinfo){
        businessService.setSpecial(businessinfo);
    }

    //逻辑删除商机
    @MyLog(type = "商机",value = "删除了商机")
    @RequestMapping("/delete_business")
    @ResponseBody
    public void deleteBusiness(String business){
        businessService.deleteBusiness(business);
    }


    /**
     * 新产品
     */
    @RequestMapping("/newproduct_info")
    @ResponseBody
    public  List<Zproduct> zproductList(String productName,Integer pcId, Integer pbId ){
        List<Zproduct> zproducts = businessService.newProductInfo(productName,pcId,pbId);
        return zproducts;
    }
}

